package com.droidclaimsurveyor.dal;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.droidclaimsurveyor.model.CarImageModel;

public class CarImageDal implements DAL <CarImageModel, String>{
	public static String GetAllCarImageId = "GetAllCarImage";
	public static String GetLastCarImageId = "GetLastCarImage";
	public static String GetCarImageByRowId = "GetCarImageByRow";
	
	private CarImageDBHelper dbHelper;
	private SQLiteDatabase db;
	private Context context;

	public CarImageDal(Context context) {
		this.context = context;
		dbHelper = new CarImageDBHelper(context,
				CarImageDBHelper.DATABASE_NAME, null,
				CarImageDBHelper.DATABASE_VERSION);
	}
	
	public List<CarImageModel> getAll(String taskId,String... parameters){
		List<CarImageModel> result = null;
		if(taskId.equals(CarImageDal.GetAllCarImageId)){
			result = getAllCarImage(parameters);
		}else if(taskId.equals(CarImageDal.GetCarImageByRowId)){
			result = getAllCarImageByRowId(parameters);
		}else if(taskId.equals(CarImageDal.GetLastCarImageId)){
			result = getLastCarImage(parameters);
		}
		
		return result;
	}
	
	public List<CarImageModel> getAllCarImage(String... claimIds){
		
		List<CarImageModel> result = new ArrayList<CarImageModel>();
		try{
			String claimId = claimIds[0];
			Cursor cursor = getCursorByClaimId(claimId);
			if (cursor.moveToFirst()) {
				do {
					String dbClaimId = cursor.getString(cursor.getColumnIndex(CarImageDBHelper.KEY_CLAIM_ID));
					if(claimId.equals(dbClaimId)){
						CarImageModel model = new CarImageModel();
						
						long rowId = cursor.getLong(cursor.getColumnIndex(CarImageDBHelper.KEY_ID));
						long id = cursor.getLong(cursor.getColumnIndex(CarImageDBHelper.KEY_CARIMAGE_ID));
						int claim_Id = cursor.getInt(cursor.getColumnIndex(CarImageDBHelper.KEY_CLAIM_ID));
						String imgPath = cursor.getString(cursor.getColumnIndex(CarImageDBHelper.KEY_IMAGE_PATH));
						
						model.setRowId(rowId);
						model.setClaim_Id(claim_Id);
						model.setId(id);
						model.setImgPath(imgPath);
						
						result.add(model);
					}
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
			
		}
		
		return result;
	}

	public List<CarImageModel> getAllCarImageByRowId(String... rowIdStr){
		
		List<CarImageModel> result = new ArrayList<CarImageModel>();
		try{
			String rowId = rowIdStr[0];
			Cursor cursor = getCursorByRowId(rowId);
			if (cursor.moveToFirst()) {
				do {
					long dbRowId = cursor.getLong(cursor.getColumnIndex(CarImageDBHelper.KEY_ID));
					if(rowIdStr.equals(Long.toString(dbRowId))){
						CarImageModel model = new CarImageModel();
						
						long id = cursor.getLong(cursor.getColumnIndex(CarImageDBHelper.KEY_CARIMAGE_ID));
						int claim_Id = cursor.getInt(cursor.getColumnIndex(CarImageDBHelper.KEY_CLAIM_ID));
						String imgPath = cursor.getString(cursor.getColumnIndex(CarImageDBHelper.KEY_IMAGE_PATH));
						
						model.setRowId(dbRowId);
						model.setClaim_Id(claim_Id);
						model.setId(id);
						model.setImgPath(imgPath);
						
						result.add(model);
					}
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
		}
		
		return result;
	}
	
	public List<CarImageModel> getLastCarImage(String... claimIds){
		
		List<CarImageModel> result = new ArrayList<CarImageModel>();
		try{
			String claimId = claimIds[0];
			Cursor cursor = getLastRecord(claimId);
			if (cursor.moveToFirst()) {
				do {
					String dbClaimId = cursor.getString(cursor.getColumnIndex(CarImageDBHelper.KEY_CLAIM_ID));
					if(claimId.equals(dbClaimId)){
						CarImageModel model = new CarImageModel();
						
						long rowId = cursor.getLong(cursor.getColumnIndex(CarImageDBHelper.KEY_ID));
						long id = cursor.getLong(cursor.getColumnIndex(CarImageDBHelper.KEY_CARIMAGE_ID));
						int claim_Id = cursor.getInt(cursor.getColumnIndex(CarImageDBHelper.KEY_CLAIM_ID));
						String imgPath = cursor.getString(cursor.getColumnIndex(CarImageDBHelper.KEY_IMAGE_PATH));
						
						model.setRowId(rowId);
						model.setClaim_Id(claim_Id);
						model.setId(id);
						model.setImgPath(imgPath);
						
						result.add(model);
					}
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
		}
		
		return result;
	}	
	
	public void close() {
		dbHelper.close();
	}

	public void open() {
		db = null;
		try {
			db = dbHelper.getWritableDatabase();
		} catch (Exception ex) {
			db = dbHelper.getReadableDatabase();
		}
	}

	public long add(CarImageModel carImage) {
		long rowIndex = -1;

		try {			
			ContentValues data = new ContentValues();
			
			data.put(CarImageDBHelper.KEY_CARIMAGE_ID, carImage.getId());
			data.put(CarImageDBHelper.KEY_CLAIM_ID, carImage.getClaim_Id());
			data.put(CarImageDBHelper.KEY_IMAGE_PATH,carImage.getImgPath());

			// return row id to caller
			rowIndex = db.insert(CarImageDBHelper.DATABASE_TABLE, null, data);
		} catch (Exception ex) {
			Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
		}

		return rowIndex;
	}

	public boolean update(long rowIndex, CarImageModel carImage) {		
		ContentValues newValue = new ContentValues();
		
		newValue.put(CarImageDBHelper.KEY_CARIMAGE_ID, carImage.getId());
		newValue.put(CarImageDBHelper.KEY_CLAIM_ID, carImage.getClaim_Id());
		newValue.put(CarImageDBHelper.KEY_IMAGE_PATH,carImage.getImgPath());

		
		return db.update(CarImageDBHelper.DATABASE_TABLE, newValue, CarImageDBHelper.KEY_ID + "=" + rowIndex,
				null) > 0;
	}

	public boolean remove(long index) {
		return db.delete(CarImageDBHelper.DATABASE_TABLE,
				CarImageDBHelper.KEY_ID + "=" + index, null) > 0;
	}

	public Cursor getAllCursor(String orderBy) {
		String columnName[] = new String[] { CarImageDBHelper.KEY_ID,
				CarImageDBHelper.KEY_CARIMAGE_ID,
				CarImageDBHelper.KEY_CLAIM_ID,
				CarImageDBHelper.KEY_IMAGE_PATH
				};

		return db.query(CarImageDBHelper.DATABASE_TABLE, columnName, null,
				null, null, null, orderBy);
	}

	public Cursor getCursorByClaimId(String claimIdStr){
		String columnName[] = new String[] { CarImageDBHelper.KEY_ID,
				CarImageDBHelper.KEY_CARIMAGE_ID,
				CarImageDBHelper.KEY_CLAIM_ID,
				CarImageDBHelper.KEY_IMAGE_PATH
			};
		
		String selection = CarImageDBHelper.KEY_CLAIM_ID + " = ?";
		String[] selectionArgs = new String[] {claimIdStr};
		return db.query(CarImageDBHelper.DATABASE_TABLE, columnName, selection,selectionArgs, null, null, null);
	}


	public Cursor getCursorByRowId(String rowIdStr){
		String columnName[] = new String[] { CarImageDBHelper.KEY_ID,
				CarImageDBHelper.KEY_CARIMAGE_ID,
				CarImageDBHelper.KEY_CLAIM_ID,
				CarImageDBHelper.KEY_IMAGE_PATH
		};
		
		String selection = CarImageDBHelper.KEY_ID + " = ?";
		String[] selectionArgs = new String[] {rowIdStr};
		return db.query(CarImageDBHelper.DATABASE_TABLE, columnName, selection,selectionArgs, null, null, null);
	}
	
	public Cursor getLastRecord(String claimId){
		String sql = "SELECT * FROM ? where ?=? ORDER BY ? DESC LIMIT 1";
		String[] selectionArgs = new String[] {
				CarImageDBHelper.DATABASE_TABLE,
				CarImageDBHelper.KEY_CLAIM_ID,
				claimId,
				CarImageDBHelper.KEY_ID
				};
		return db.rawQuery(sql,selectionArgs);
	}
}
